<template>
  <el-dialog :visible.sync="visible" v-if="visible" :title="title" append-to-body>
    <el-form :model="formData" :rules="rules" ref="formRef" label-width="80px">
      <el-form-item label="章节名称" prop="catalogName">
        <el-input v-model="formData.catalogName"  placeholder="章节名称"/>
      </el-form-item>
      <el-form-item label="章节内容">
        <vue-editor ref="vueEditor" v-model="formData.content"/>
      </el-form-item>
      <el-form-item label="章节排序" prop="orderNo">
        <el-input v-model="formData.orderNo"  placeholder="章节排序"/>
      </el-form-item>
      <el-form-item label="章节状态">
        <el-radio-group v-model="formData.catalogStatus">
          <el-radio :label="1">发表</el-radio>
          <el-radio :label="2">草稿</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="发表时间" v-if="formData.catalogStatus === 1">
        <el-date-picker type="datetime" v-model="formData.applyTime" placeholder="选择发表时间"></el-date-picker>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="formData.status">
          <el-radio :label="1">可用</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button type="info" @click="showContent = true">预览</el-button>
        <el-button type="warning" @click="visible = false">取消</el-button>
      </el-form-item>
    </el-form>
    <div class="ql-container ql-snow" v-if="showContent">
      <div class="ql-editor" v-html="formData.content"></div>
    </div>
  </el-dialog>
</template>

<script>
const formData = {
  id: '',
  catalogName: '',
  content: '',
  bookId: '',
  catalogStatus: 1,
  applyTime: '',
  orderNo: 1,
  status: 1
}
export default {
  name: 'CatalogUpdate',
  data () {
    return {
      showContent: false,
      visible: false,
      title: '添加数据',
      formData: {
        ...formData
      },
      rules: {
        catalogName: [{
          required: true,
          message: '请输入章节名称'
        }],
        orderNo: [{
          required: true,
          message: '请输入排序值'
        }]
      }
    }
  },
  methods: {
    init (id, bookId) {
      this.visible = true
      this.title = id ? '修改数据' : '添加数据'
      this.formData = { ...formData }
      this.formData.bookId = bookId
      if (id) {
        this.$http.get(`book/catalog/info/${id}`).then(data => {
          if (data) {
            this.formData = { ...data }
          }
        })
      }
    },
    submit () {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          if (this.formData.catalogStatus === 1) {
            if (!this.formData.applyTime) {
              this.formData.applyTime = new Date()
            }
          } else {
            this.formData.applyTime = ''
          }
          this.$http.post('book/catalog/update', this.formData).then(() => {
            this.$message.success('操作成功')
            this.visible = false
            this.$emit('closeDialog')
          }).catch(() => {})
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
